package com.qumitech.visualnovel.controller.rest;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.qumitech.visualnovel.model.common.BaseResponse;
import com.qumitech.visualnovel.model.dao.console.SpiderSource;
import com.qumitech.visualnovel.service.content.SpiderSourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/spiderSource")
public class SpiderSourceController {

    @Autowired
    SpiderSourceService spiderSourceService;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public JSONObject getList(@RequestParam(value="startTime", required=false) String startTime,
                              @RequestParam(value="endTime", required=false) String endTime,
                              @RequestParam(value="offset", required=false, defaultValue="0") int offset,
                              @RequestParam(value="limit", required=false, defaultValue="20") int limit,
                              @RequestParam(value="orderType", required=false, defaultValue="DESC") String orderType,
                              @RequestParam(value="orderBy", required=false, defaultValue="update_time") String orderBy) {

        JSONObject data = new JSONObject();
        int count = spiderSourceService.getCount(startTime, endTime);
        data.put("count", count);
        JSONArray dataItems = new JSONArray();
        List<SpiderSource> spiderSourceServiceList = spiderSourceService.getList(startTime, endTime, offset, limit, orderType, orderBy);
        for(SpiderSource spiderSource: spiderSourceServiceList)
        {
            dataItems.add(spiderSource.toJsonObject());
        }
        data.put("items", dataItems);
        BaseResponse results = new BaseResponse(data);
        return results;
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public JSONObject add(@RequestBody JSONObject param){
        return  spiderSourceService.add(param);
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public JSONObject update(@RequestBody JSONObject param){
        return  spiderSourceService.update(param);
    }

    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public JSONObject delete(@RequestParam(value="id") Long id){
        return  spiderSourceService.delete(id);
    }



}
